package com.hualan.sfzy.controller;

import com.hualan.sfzy.model.dto.CharityCenterDto;
import com.hualan.sfzy.model.entity.CharityCenter;
import com.hualan.sfzy.model.vo.CharityCenterQuery;
import com.hualan.sfzy.service.CharityCenterService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.hualan.sfzy.constant.CommonConstant.*;

/**
 * 慈善中心管理控制器
 */
@RestController
@RequestMapping("/charityCenter")
public class CharityCenterController {

    @Autowired
    private CharityCenterService charityCenterService;

    /**
     * 添加慈善中心
     */
    @PostMapping("/add")
    public Result add(@RequestBody CharityCenter charityCenter) {
        int result = charityCenterService.add(charityCenter);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 编辑慈善中心
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody CharityCenter charityCenter) {
        int result = charityCenterService.edit(charityCenter);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 删除慈善中心
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        int result = charityCenterService.remove(id);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 批量删除慈善中心
     */
    @DeleteMapping("/removeBatch")
    public Result removeBatch(@RequestBody Integer[] ids) {
        int result = charityCenterService.removeBatch(ids);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 查询所有慈善中心
     */
    @GetMapping("/find")
    public Result find() {
        List<CharityCenterDto> charityCenters = charityCenterService.findAll();
        return Result.success(charityCenters);
    }


    /**
     * 根据ID查询慈善中心
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        CharityCenter charityCenter = charityCenterService.findById(id);
        return Result.success(charityCenter);
    }

    /**
     * 分页条件查询慈善中心
     */
    @GetMapping("/findCenterPage")
    public Result findCenterPage(@RequestParam(value = PAGE_NUM, defaultValue = PAGE_NUM_DEFAULT) Integer pageNum,
                                @RequestParam(value = PAGE_SIZE, defaultValue = PAGE_SIZE_DEFAULT) Integer pageSize,
                                CharityCenterQuery query) {
        Map<String, Object> result = charityCenterService.findCenterPage(pageNum, pageSize, query);
        return Result.success(result);
    }
} 